var DocType = function() {
	this.init = function() {
			$("#addDocType").live(
						'click',
						function() {
							var myts = new Date();
							var formStr = $('#doctype_filter').serialize();
							var pk_org = $("#vendorOrg").val();
							var pk_group = $("#vendorGroup").val();
							if((pk_org==null||pk_org=="")){
								showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
								return;
							}
							jQuery.ajax( {
								type : "GET",
								async : false,
								url : ebvp_context + "/register/getDocType?myts="+myts+"&pk_group="+pk_group+"&pk_org="+pk_org,
								dataType : "text",
								data : "&" + formStr,
								success : function(result) {
									if (result != null && result != "[]") {
										var arr = eval(result);
										$("#doctype_list").empty();
										for (var loop = 0; loop < arr.length; loop++) {
											var obj= arr[loop];
											var pk = $("<input/>").val(obj.pk_defdoc).attr("type", "hidden").attr("name", "pk_defdoc_intree");
											var checkBox = $("<input/>")
																	.val(obj.pk_defdoc)
																	.attr("type", "checkbox").attr("name", "pkcheckbox");
											var name = $("<label></label>")
															.text(obj.name).attr("title", "doctypename");
											var div = $("<div></div>").append(pk).append(checkBox).append(name);
											var existDocType = $("#complateDocType").find("[name='finfotype']");
											// check unique
											if(existDocType != null && existDocType.length !=0){
												for(var l=0; l<existDocType.length; l++){
													if($(existDocType[l]).val() == arr[loop].pk_defdoc){
														break;
													}
												}
												if(l == existDocType.length){
													$("#doctype_list").append(div);
												}
											}else{
												$("#doctype_list").append(div);
											}
										}
									}else{
										$("#doctype_list").empty();
									}
								}
					});
					$("#docTypePicker").slideDown("normal");
					var position = $("#addDocType").offset();
					position.top += 20;
					$("#docTypePicker").offset(position);
				});
		
		$("#filterDocType")
				.live(
						'click',
						function() {
							var myts = new Date();
							var encodeFiltername = escape(encodeURIComponent($("#doctype_filter").val()));
							var pk_org = $("#vendorOrg").val();
							var pk_group = $("#vendorGroup").val();
							if((pk_org==null||pk_org=="")){
								showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
								return;
							}
							jQuery
									.ajax( {
										type : "GET",
										async : false,
										url : ebvp_context
												+ "/register/getDocType?myts="
												+ myts+"&doctype_filter="+encodeFiltername+"&pk_group="+pk_group+"&pk_org="+pk_org,
										dataType : "text",
										success : function(result) {
											if (result != null && result != "[]") {
												var arr = eval(result);
												$("#doctype_list").empty();
												for (var loop = 0; loop < arr.length; loop++) {
													var obj= arr[loop];
													var pk = $("<input/>").val(obj.pk_defdoc).attr("type", "hidden").attr("name", "pk_defdoc_intree");
													var checkBox = $("<input/>")
																			.val(obj.pk_defdoc)
																			.attr("type", "checkbox").attr("name", "pkcheckbox");
													var name = $("<label></label>")
																	.text(obj.name).attr("title", "doctypename");
													var div = $("<div></div>").append(pk).append(checkBox).append(name);
													var existDocType = $("#complateDocType").find("[name='finfotype']");
													// check unique
													if(existDocType != null && existDocType.length !=0){
														for(var l=0; l<existDocType.length; l++){
															if($(existDocType[l]).val() == arr[loop].pk_defdoc){
																break;
															}
														}
														if(l == existDocType.length){
															$("#doctype_list").append(div);
														}
													}else{
														$("#doctype_list").append(div);
													}
												}
											}else{
												$("#doctype_list").empty();
											}
										}
									});
						});
		
		$("body").delegate("[name='doctypePicked']", "click", function() {
			var items = $("#doctype_list").find("[name='pkcheckbox']");
			if (items != null && items.length > 0) {
				for (var loop = 0; loop < items.length; loop++) {
					if(items[loop].checked == true){
						var newrow = $("#doctype_tpl").find(".idItems").clone();
						var pk = $(items[loop]).prev("input").val();
						var name = $(items[loop]).next("label").text();
						$(newrow.find("[name='finfotype']")[0]).val(pk);
						$(newrow.find("[name='finfotypeName']")[0]).val(name);
						$(newrow.find("[name='addDocTypeAttachment']")[0]).attr("id", "addDocTypeAttachment_" + pk);
						
						newrow.insertBefore($("#addDocType"));
					}
				}
			}
			$("#docTypePicker").slideUp();
			return false;
		});
		
		$("body").delegate("[name='doctypeCancel']", "click", function() {
			$("#docTypePicker").slideUp();
			return false;
		});
	};
};

$(function() {
	var docType = new DocType();
	docType.init();
});